{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"faceId\": \"b2b46d7e-0784-47b3-9b9b-ed2dd332952b\", \"faceRectangle\": {\"top\": 402, \"left\": 132, \"width\": 649, \"height\": 649}, \"faceAttributes\": {\"smile\": 0.808, \"headPose\": {\"pitch\": -16.7, \"roll\": -3.7, \"yaw\": -4.6}, \"gender\": \"female\", \"age\": 29.0, \"facialHair\": {\"moustache\": 0.0, \"beard\": 0.0, \"sideburns\": 0.0}, \"glasses\": \"NoGlasses\", \"emotion\": {\"anger\": 0.186, \"contempt\": 0.001, \"disgust\": 0.006, \"fear\": 0.001, \"happiness\": 0.808, \"neutral\": 0.0, \"sadness\": 0.0, \"surprise\": 0.0}, \"blur\": {\"blurLevel\": \"medium\", \"value\": 0.27}, \"exposure\": {\"exposureLevel\": \"goodExposure\", \"value\": 0.66}, \"noise\": {\"noiseLevel\": \"low\", \"value\": 0.0}, \"makeup\": {\"eyeMakeup\": true, \"lipMakeup\": false}, \"accessories\": [], \"occlusion\": {\"foreheadOccluded\": false, \"eyeOccluded\": false, \"mouthOccluded\": false}, \"hair\": {\"bald\": 0.15, \"invisible\": false, \"hairColor\": [{\"color\": \"brown\", \"confidence\": 0.93}, {\"color\": \"black\", \"confidence\": 0.81}, {\"color\": \"blond\", \"confidence\": 0.38}, {\"color\": \"gray\", \"confidence\": 0.28}, {\"color\": \"other\", \"confidence\": 0.25}, {\"color\": \"red\", \"confidence\": 0.18}, {\"color\": \"white\", \"confidence\": 0.0}]}}}]'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"5b687b7615fa42e091a14868f10e1eb3\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://renlian01.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://gitee.com/forevercan/resume/raw/master/assets/images/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201016161936.jpg'\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "json.dumps(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![开心的郭老师](https://gitee.com/forevercan/resume/raw/master/assets/images/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201016161936.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'b2b46d7e-0784-47b3-9b9b-ed2dd332952b',\n",
       "  'faceRectangle': {'top': 402, 'left': 132, 'width': 649, 'height': 649},\n",
       "  'faceAttributes': {'smile': 0.808,\n",
       "   'headPose': {'pitch': -16.7, 'roll': -3.7, 'yaw': -4.6},\n",
       "   'gender': 'female',\n",
       "   'age': 29.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.186,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.006,\n",
       "    'fear': 0.001,\n",
       "    'happiness': 0.808,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.27},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.66},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.15,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.93},\n",
       "     {'color': 'black', 'confidence': 0.81},\n",
       "     {'color': 'blond', 'confidence': 0.38},\n",
       "     {'color': 'gray', 'confidence': 0.28},\n",
       "     {'color': 'other', 'confidence': 0.25},\n",
       "     {'color': 'red', 'confidence': 0.18},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = response.json()\n",
    "results"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
